package com.manfred.uddi.test;

import java.util.Vector;

import org.uddi4j.client.UDDIProxy;
import org.uddi4j.datatype.OverviewDoc;
import org.uddi4j.datatype.OverviewURL;
import org.uddi4j.datatype.binding.AccessPoint;
import org.uddi4j.datatype.binding.BindingTemplate;
import org.uddi4j.datatype.binding.BindingTemplates;
import org.uddi4j.datatype.binding.InstanceDetails;
import org.uddi4j.datatype.binding.TModelInstanceDetails;
import org.uddi4j.datatype.binding.TModelInstanceInfo;
import org.uddi4j.datatype.service.BusinessService;
import org.uddi4j.datatype.tmodel.TModel;
import org.uddi4j.response.AuthToken;
import org.uddi4j.response.ServiceDetail;
import org.uddi4j.transport.TransportFactory;
import org.uddi4j.util.CategoryBag;
import org.uddi4j.util.KeyedReference;

public class CreateNewService {

	public static void main(String args[]) {
		// String inquiryURL = "http://localhost:8080/juddi/inquiry";
		// String publishURL = "http://localhost:8080/juddi/publish";
		String inquiryURL = "http://localhost:8080/juddiv3/services/inquiryv2";
		String publishURL = "http://localhost:8080/juddiv3/services/publishv2";

		String userId = "juddi";

		String credential = "juddi";
		// String businessKey = "1D359E20-D899-11DA-A5BF-F9EAF6D4E3F7";
		// String businessKey =
		// "uddi:juddi.apache.org:c81075d0-78ca-4c80-b605-56e5976bee34";
		String businessKey = "uddi:juddi.apache.org:86e0e729-fce9-4ce0-a0c8-2d3aca07ca01";

		String transportClass = "org.uddi4j.transport.ApacheAxisTransport";
		System.setProperty(TransportFactory.PROPERTY_NAME, transportClass);

		UDDIProxy proxy = new UDDIProxy();

		try {

			proxy.setInquiryURL(inquiryURL);
			proxy.setPublishURL(publishURL);

			AuthToken token = proxy.get_authToken(userId, credential);
			System.out.println("Security authToken:"
					+ token.getAuthInfoString());

			BusinessService businessService = new BusinessService("");
			businessService.setDefaultNameString("ClassifiedService", null);
			businessService.setBusinessKey(businessKey);

			BindingTemplate bindingTemplate = new BindingTemplate();

			AccessPoint accessPoint = new AccessPoint(
					"http://www.daily-moon.com:8080/axis2/services/ClassifiedService",
					"http");
			bindingTemplate.setAccessPoint(accessPoint);

			TModelInstanceDetails tModelDetails = new TModelInstanceDetails();
			TModelInstanceInfo tModelInstanceInfo = new TModelInstanceInfo(
					"332F67D0-D8A1-11DA-A5BF-DF15F533BFA1");

			InstanceDetails instanceDetails = new InstanceDetails();
			OverviewDoc overviewDoc = new OverviewDoc();
			OverviewURL overviewURL = new OverviewURL(
					"http://www.nicholaschase.com/ClassifiedService-impl.wsdl");
			overviewDoc.setOverviewURL(overviewURL);
			instanceDetails.setOverviewDoc(overviewDoc);

			tModelInstanceInfo.setInstanceDetails(instanceDetails);

			tModelDetails.add(tModelInstanceInfo);
			bindingTemplate.setTModelInstanceDetails(tModelDetails);
			bindingTemplate.setBindingKey("ClassifiedService");

			BindingTemplates bindingTemplates = new BindingTemplates();
			bindingTemplates.add(bindingTemplate);
			businessService.setBindingTemplates(bindingTemplates);

			KeyedReference wsdlNotation = new KeyedReference(
					TModel.TYPES_TMODEL_KEY, "wsdlSpec",
					"C1ACF26D-9672-4404-9D70-39B756E62AB4");
			KeyedReference typeNotation = new KeyedReference(
					"ntis-gov:naics:1997", "511110",
					"C0B9FE13-179F-413D-8A5B-5004DB8E5BB2");
			CategoryBag catBag = new CategoryBag();
			catBag.add(wsdlNotation);
			catBag.add(typeNotation);
			// businessService.setCategoryBag(catBag);

			Vector services = new Vector();
			services.addElement(businessService);

			ServiceDetail serviceDetail = proxy.save_service(
					token.getAuthInfoString(), services);

			Vector businessServices = serviceDetail.getBusinessServiceVector();
			BusinessService businessServiceReturned = (BusinessService) (businessServices
					.elementAt(0));

			String serviceKey = businessServiceReturned.getServiceKey();

			System.out.println("The Name: "
					+ businessServiceReturned.getDefaultNameString());
			System.out.println("The ServiceKey: " + serviceKey);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}